package com.elite.groovy.callback

import com.elite.groovy.davco.clink.ClinkUtils
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat

/**
 * @projectName: Groovy
 * @package: com.elite.groovy.callback
 * @className: apiClinkCdrRecord
 * @description: 同步天润IB呼入&OB呼出记录数据
 * @author: LiJT
 * @date: 2023-10-27 11:13
 * @version: 1.0
 */

public class apiClinkCdrRecord implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiClinkCdrRecord.class);

    private static boolean isRun = true;

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        try {

            // 获取数据源
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

            String SelectIBSQL = '''Select Uniqueid
                                      From Clink_List_Cdr_Ibs
                                     Where (Callid Is Null Or Callid = '')
                                       And Sync_Time > Date_Add(Now(), Interval - 48 Hour)
                                     Order By Starttime, Sync_Time Limit 200''';
            List<Map<String, Object>> IB_List = jt.queryForList(SelectIBSQL);
            log.debug("IB通话数据-待同步任务:" + IB_List.size());

            for (int i = 0; i < IB_List.size(); i++) {
                String Uniqueid = IB_List.get(i).get("Uniqueid").toString();
                toDoIBGet(jt, Uniqueid);
            }

            String SelectOBSQL = '''Select Uniqueid
                                      From Clink_List_Cdr_Obs
                                     Where (Callid Is Null Or Callid = '')
                                       And Sync_Time > Date_Add(Now(), Interval - 48 Hour)
                                     Order By Starttime, Sync_Time Limit 200''';
            List<Map<String, Object>> OB_List = jt.queryForList(SelectOBSQL);
            log.debug("OB通话数据-待同步任务:" + OB_List.size());

            for (int i = 0; i < OB_List.size(); i++) {
                String Uniqueid = OB_List.get(i).get("Uniqueid").toString();
                toDoOBGet(jt, Uniqueid);
            }

            log.debug("通话数据同步任务结束执行...");
        } catch (Exception e) {
            log.error("HTTP请求调用错误，接口处理异常：" + e.getMessage());
            return e.getMessage();

        }

        return "SUCC";

    }

    // 1-呼入通话记录
    static toDoIBGet(JdbcTemplate jt, String mainUniqueId) {

        //处理主数据
        log.debug("1-呼入通话记录 toDoGet Data mainUniqueId:" + mainUniqueId);

        // 封装调用头Heads
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/json");

        // 系统接入-GET
        String getPath = "/cc/list_cdr_ibs?";

        Map<String, String> queryParams = new HashMap<String, String>();
        queryParams.put("mainUniqueId", String.valueOf(mainUniqueId));
        String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
        log.debug("系统接入 doGETRes:" + doGETRes);

        JSONObject doGETJSON = JSONObject.fromObject(doGETRes);
        log.debug("1-呼入通话记录 Data Result:" + doGETJSON);

        JSONArray dataArray = doGETJSON.getJSONArray("cdrIbs"); ;
        int dataArrayLen = dataArray.size();
        log.info("1-呼入通话记录 Data Result size:" + dataArrayLen);

        JSONObject dataObject = null;
        String sql = null;
        List sqlResult = null;
        int Countnum = 0;
        for (int i = 0; i < dataArrayLen; i++) {
            dataObject = dataArray.getJSONObject(i);
            String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
            String hotline = KHUtil.GetSFJV(dataObject, "hotline");
            String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
            String customerNumberEncrypt = KHUtil.GetSFJV(dataObject, "customerNumberEncrypt");
            String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
            String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
            String qno = KHUtil.GetSFJV(dataObject, "qno");
            String qname = KHUtil.GetSFJV(dataObject, "qname");
            String cno = KHUtil.GetSFJV(dataObject, "cno");
            String clientName = KHUtil.GetSFJV(dataObject, "clientName");
            String clientNumber = KHUtil.GetSFJV(dataObject, "clientNumber");
            String clientRingingTime = KHUtil.GetSFJV(dataObject, "clientRingingTime");
            String callType = KHUtil.GetSFJV(dataObject, "callType");
            String startTime = KHUtil.GetSFJV(dataObject, "startTime");
            String endTime = KHUtil.GetSFJV(dataObject, "endTime");
            String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
            String bridgeDuration = KHUtil.GetSFJV(dataObject, "bridgeDuration");
            String totalDuration = KHUtil.GetSFJV(dataObject, "totalDuration");
            String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
            String status = KHUtil.GetSFJV(dataObject, "status");
            String statusCode = KHUtil.GetSFJV(dataObject, "statusCode");
            String endReason = KHUtil.GetSFJV(dataObject, "endReason");
            String sipCause = KHUtil.GetSFJV(dataObject, "sipCause");
            String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");
            String userField = KHUtil.GetSFJV(dataObject, "userField");
            String mark = KHUtil.GetSFJV(dataObject, "mark");
            String tags = KHUtil.GetSFJV(dataObject, "tags");
            String markData = KHUtil.GetSFJV(dataObject, "markData");
            String joinQueueTime = KHUtil.GetSFJV(dataObject, "joinQueueTime");
            String leaveQueueTime = KHUtil.GetSFJV(dataObject, "leaveQueueTime");
            String ibWaitDuration = KHUtil.GetSFJV(dataObject, "ibWaitDuration");
            String sayVoiceDuration = KHUtil.GetSFJV(dataObject, "sayVoiceDuration");
            String clientOffhookTime = KHUtil.GetSFJV(dataObject, "clientOffhookTime");
            String ibRingingDuration = KHUtil.GetSFJV(dataObject, "ibRingingDuration");
            String queueAnswerInTime = KHUtil.GetSFJV(dataObject, "queueAnswerInTime");
            String evaluation = KHUtil.GetSFJV(dataObject, "evaluation");
            String hotlineName = KHUtil.GetSFJV(dataObject, "hotlineName");
            String firstJoinQueueTime = KHUtil.GetSFJV(dataObject, "firstJoinQueueTime");
            String callId = KHUtil.GetSFJV(dataObject, "callId");
            // log.debug("Deal Data uniqueId:" + uniqueId);

            // 检查当前数据库是否存在 存在更新 不存在新建
            sql = "Select Count(*) As Count From clink_list_cdr_ibs Where uniqueId = ?";
            sqlResult = jt.queryForList(sql, uniqueId);
            Countnum = sqlResult.get(0).get("Count");
            log.debug("1-呼入通话记录 检查当前数据库是否存在:" + Countnum);

            if (Countnum <= 0) {

                sql = '''INSERT Into clink_list_cdr_ibs
                          (uniqueId,
                           hotline,
                           customerNumber,
                           customerNumberEncrypt,
                           customerProvince,
                           customerCity,
                           qno,
                           qname,
                           cno,
                           clientName,
                           clientNumber,
                           clientRingingTime,
                           callType,
                           startTime,
                           endTime,
                           bridgeTime,
                           bridgeDuration,
                           totalDuration,
                           ivrName,
                           status,
                           statusCode,
                           endReason,
                           sipCause,
                           recordFile,
                           userField,
                           mark,
                           tags,
                           markData,
                           joinQueueTime,
                           leaveQueueTime,
                           ibWaitDuration,
                           sayVoiceDuration,
                           clientOffhookTime,
                           ibRingingDuration,
                           queueAnswerInTime,
                           evaluation,
                           hotlineName,
                           firstJoinQueueTime,
                           callId)
                        VALUES
                          (\'''' + uniqueId + '''\',
                           \'''' + hotline + '''\',
                           \'''' + customerNumber + '''\',
                           \'''' + customerNumberEncrypt + '''\',
                           \'''' + customerProvince + '''\',
                           \'''' + customerCity + '''\',
                           \'''' + qno + '''\',
                           \'''' + qname + '''\',
                           \'''' + cno + '''\',
                           \'''' + clientName + '''\',
                           \'''' + clientNumber + '''\',
                           \'''' + clientRingingTime + '''\',
                           \'''' + callType + '''\',
                           \'''' + startTime + '''\',
                           \'''' + endTime + '''\',
                           \'''' + bridgeTime + '''\',
                           \'''' + bridgeDuration + '''\',
                           \'''' + totalDuration + '''\',
                           \'''' + ivrName + '''\',
                           \'''' + status + '''\',
                           \'''' + statusCode + '''\',
                           \'''' + endReason + '''\',
                           \'''' + sipCause + '''\',
                           \'''' + recordFile + '''\',
                           \'''' + userField + '''\',
                           \'''' + mark + '''\',
                           \'''' + tags + '''\',
                           \'''' + markData + '''\',
                           \'''' + joinQueueTime + '''\',
                           \'''' + leaveQueueTime + '''\',
                           \'''' + ibWaitDuration + '''\',
                           \'''' + sayVoiceDuration + '''\',
                           \'''' + clientOffhookTime + '''\',
                           \'''' + ibRingingDuration + '''\',
                           \'''' + queueAnswerInTime + '''\',
                           \'''' + evaluation + '''\',
                           \'''' + hotlineName + '''\',
                           \'''' + firstJoinQueueTime + '''\',
                           \'''' + callId + '''\')''';

                jt.update(sql);

            } else {
                sql = '''Update clink_list_cdr_ibs
                           SET hotline               = \'''' + hotline + '''\',
                               customerNumber        = \'''' + customerNumber + '''\',
                               customerNumberEncrypt = \'''' + customerNumberEncrypt + '''\',
                               customerProvince      = \'''' + customerProvince + '''\',
                               customerCity          = \'''' + customerCity + '''\',
                               qno                   = \'''' + qno + '''\',
                               qname                 = \'''' + qname + '''\',
                               cno                   = \'''' + cno + '''\',
                               clientName            = \'''' + clientName + '''\',
                               clientNumber          = \'''' + clientNumber + '''\',
                               clientRingingTime     = \'''' + clientRingingTime + '''\',
                               callType              = \'''' + callType + '''\',
                               startTime             = \'''' + startTime + '''\',
                               endTime               = \'''' + endTime + '''\',
                               bridgeTime            = \'''' + bridgeTime + '''\',
                               bridgeDuration        = \'''' + bridgeDuration + '''\',
                               totalDuration         = \'''' + totalDuration + '''\',
                               ivrName               = \'''' + ivrName + '''\',
                               status                = \'''' + status + '''\',
                               statusCode            = \'''' + statusCode + '''\',
                               endReason             = \'''' + endReason + '''\',
                               sipCause              = \'''' + sipCause + '''\',
                               recordFile            = \'''' + recordFile + '''\',
                               userField             = \'''' + userField + '''\',
                               mark                  = \'''' + mark + '''\',
                               tags                  = \'''' + tags + '''\',
                               markData              = \'''' + markData + '''\',
                               joinQueueTime         = \'''' + joinQueueTime + '''\',
                               leaveQueueTime        = \'''' + leaveQueueTime + '''\',
                               ibWaitDuration        = \'''' + ibWaitDuration + '''\',
                               sayVoiceDuration      = \'''' + sayVoiceDuration + '''\',
                               clientOffhookTime     = \'''' + clientOffhookTime + '''\',
                               ibRingingDuration     = \'''' + ibRingingDuration + '''\',
                               queueAnswerInTime     = \'''' + queueAnswerInTime + '''\',
                               evaluation            = \'''' + evaluation + '''\',
                               hotlineName           = \'''' + hotlineName + '''\',
                               firstJoinQueueTime    = \'''' + firstJoinQueueTime + '''\',
                               callId                = \'''' + callId + '''\'
                         WHERE uniqueId = \'''' + uniqueId + '''\'''';

                jt.update(sql);
            }
            toDoIBGetd(jt, uniqueId);
            callRepeatFix(jt, uniqueId);
        }

    }

    // 2-呼入通话记录明细
    static toDoIBGetd(JdbcTemplate jt, String mainUniqueId) {

        //处理主数据
        log.debug("2-呼入通话记录明细 toDoGet Data mainUniqueId:" + mainUniqueId);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 封装调用头Heads
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/json");

        // 系统接入-GET
        String getPath = "/cc/describe_cdr_ib?";

        Map<String, String> queryParams = new HashMap<String, String>();
        queryParams.put("mainUniqueId", mainUniqueId);
        String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
        log.debug("系统接入 doGETRes:" + doGETRes);

        JSONObject dataObject = JSONObject.fromObject(doGETRes).getJSONObject("cdrIb");
        log.debug("2-呼入通话记录明细 Data Result:" + dataObject);

        String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
        String callId = KHUtil.GetSFJV(dataObject, "callId");
        String hotline = KHUtil.GetSFJV(dataObject, "hotline");
        String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
        String customerNumberEncrypt = KHUtil.GetSFJV(dataObject, "customerNumberEncrypt");
        String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
        String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
        String qno = KHUtil.GetSFJV(dataObject, "qno");
        String cno = KHUtil.GetSFJV(dataObject, "cno");
        String clientName = KHUtil.GetSFJV(dataObject, "clientName");
        String clientNumber = KHUtil.GetSFJV(dataObject, "clientNumber");
        String clientRingingTime = KHUtil.GetSFJV(dataObject, "clientRingingTime");
        String callType = KHUtil.GetSFJV(dataObject, "callType");
        String startTime = KHUtil.GetSFJV(dataObject, "startTime");
        String endTime = KHUtil.GetSFJV(dataObject, "endTime");
        String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
        String bridgeDuration = KHUtil.GetSFJV(dataObject, "bridgeDuration");
        String totalDuration = KHUtil.GetSFJV(dataObject, "totalDuration");
        String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
        String status = KHUtil.GetSFJV(dataObject, "status");
        String statusCode = KHUtil.GetSFJV(dataObject, "statusCode");
        String endReason = KHUtil.GetSFJV(dataObject, "endReason");
        String sipCause = KHUtil.GetSFJV(dataObject, "sipCause");
        String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");
        String userField = KHUtil.GetSFJV(dataObject, "userField");
        String ivrFlows = KHUtil.GetSFJV(dataObject, "ivrFlows");
        String investigation = KHUtil.GetSFJV(dataObject, "investigation");
        String tags = KHUtil.GetSFJV(dataObject, "tags");
        String mark = KHUtil.GetSFJV(dataObject, "mark");
        String qname = KHUtil.GetSFJV(dataObject, "qname");
        String markData = KHUtil.GetSFJV(dataObject, "markData");
        String joinQueueTime = KHUtil.GetSFJV(dataObject, "joinQueueTime");
        String leaveQueueTime = KHUtil.GetSFJV(dataObject, "leaveQueueTime");
        String ibWaitDuration = KHUtil.GetSFJV(dataObject, "ibWaitDuration");
        String fastHangUp = KHUtil.GetSFJV(dataObject, "fastHangUp");
        String clientOffhookTime = KHUtil.GetSFJV(dataObject, "clientOffhookTime");
        String sayVoiceDuration = KHUtil.GetSFJV(dataObject, "sayVoiceDuration");
        String ibRingingDuration = KHUtil.GetSFJV(dataObject, "ibRingingDuration");
        String queueAnswerInTime = KHUtil.GetSFJV(dataObject, "queueAnswerInTime");
        String evaluation = KHUtil.GetSFJV(dataObject, "evaluation");
        String firstJoinQueueTime = KHUtil.GetSFJV(dataObject, "firstJoinQueueTime");
        // log.debug("2-呼入通话记录明细 Deal Data uniqueId:" + uniqueId);

        // 检查当前数据库是否存在 存在更新 不存在新建
        String sql = "Select Count(*) As Count From clink_describe_cdr_ib Where uniqueId = ?";
        List sqlResult = jt.queryForList(sql, uniqueId);
        int Countnum = sqlResult.get(0).get("Count");
        log.debug("2-呼入通话记录明细 检查当前数据库是否存在:" + Countnum);

        if (Countnum <= 0) {

            sql = '''INSERT Into clink_describe_cdr_ib (uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, qno, qname, cno, clientName, clientNumber, clientRingingTime, callType, joinQueueTime, leaveQueueTime, ibWaitDuration, callId, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, ivrFlows, investigation, tags, mark, fastHangUp, sayVoiceDuration, clientOffhookTime, ibRingingDuration, queueAnswerInTime, evaluation, firstJoinQueueTime, markData) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

            jt.update(sql, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, qno, qname, cno, clientName, clientNumber, clientRingingTime, callType, joinQueueTime, leaveQueueTime, ibWaitDuration, callId, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, ivrFlows, investigation, tags, mark, fastHangUp, sayVoiceDuration, clientOffhookTime, ibRingingDuration, queueAnswerInTime, evaluation, firstJoinQueueTime, markData);
        }

        if (!KHUtil.isNull(ivrFlows)) {
            toDoOBGetIVR(jt, uniqueId, ivrFlows);
        }

    }

    // 2-1-呼入通话记录明细-IVR
    static toDoOBGetIVR(JdbcTemplate jt, String uniqueId, String IVRData) {

        //处理主数据
        log.debug("2-1-呼入通话记录明细-IVR toDoGet Data uniqueId:" + uniqueId);

        JSONArray dataArray = JSONArray.fromObject(IVRData);
        int dataArrayLen = dataArray.size();
        log.debug("2-1-呼入通话记录明细-IVR Data Result size:" + dataArrayLen);

        String sql = "Delete From clink_describe_cdr_ib_ivr Where uniqueId = ?";
        jt.update(sql, uniqueId);

        JSONObject dataObject = null;
        for (int i = 0; i < dataArrayLen; i++) {
            dataObject = dataArray.getJSONObject(i);
            String pathName = KHUtil.GetSFJV(dataObject, "pathName");
            String path = KHUtil.GetSFJV(dataObject, "path");
            String routerStartTime = KHUtil.GetSFJV(dataObject, "routerStartTime");
            String routerEndTime = KHUtil.GetSFJV(dataObject, "routerEndTime");
            String routerName = KHUtil.GetSFJV(dataObject, "routerName");
            String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
            String action = KHUtil.GetSFJV(dataObject, "action");
            String startTime = KHUtil.GetSFJV(dataObject, "startTime");
            String endTime = KHUtil.GetSFJV(dataObject, "endTime");
            String pressKey = KHUtil.GetSFJV(dataObject, "pressKey");
            String pressTime = KHUtil.GetSFJV(dataObject, "pressTime");

            sql = '''Insert Into Clink_Describe_Cdr_Ib_Ivr
                      (Uniqueid,
                       Pathname,
                       Path,
                       Routerstarttime,
                       Routerendtime,
                       Routername,
                       Ivrname,
                       Action,
                       Starttime,
                       Endtime,
                       Presskey,
                       Presstime)
                    Values
                      (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

            jt.update(sql, uniqueId, pathName, path, routerStartTime, routerEndTime, routerName, ivrName, action, startTime, endTime, pressKey, pressTime);

        }

    }

    // 3-呼出通话记录
    static toDoOBGet(JdbcTemplate jt, String mainUniqueId) {

        //处理主数据
        log.debug("3-呼出通话记录 toDoGet Data mainUniqueId:" + mainUniqueId);

        // 封装调用头Heads
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/json");

        // 系统接入-GET
        String getPath = "/cc/list_cdr_obs?";

        Map<String, String> queryParams = new HashMap<String, String>();
        queryParams.put("mainUniqueId", String.valueOf(mainUniqueId));
        String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
        log.debug("系统接入 doGETRes:" + doGETRes);

        JSONObject doGETJSON = JSONObject.fromObject(doGETRes);
        log.debug("3-呼出通话记录 Data Result:" + doGETJSON);

        JSONArray dataArray = doGETJSON.getJSONArray("cdrObs"); ;
        int dataArrayLen = dataArray.size();
        log.debug("3-呼出通话记录 Data Result size:" + dataArrayLen);

        JSONObject dataObject = null;
        String sql = null;
        List sqlResult = null;
        int Countnum = 0;
        for (int i = 0; i < dataArrayLen; i++) {
            dataObject = dataArray.getJSONObject(i);
            String callId = KHUtil.GetSFJV(dataObject, "callId");
            String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
            String hotline = KHUtil.GetSFJV(dataObject, "hotline");
            String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
            String customerNumberEncrypt = KHUtil.GetSFJV(dataObject, "customerNumberEncrypt");
            String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
            String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
            String cno = KHUtil.GetSFJV(dataObject, "cno");
            String clientName = KHUtil.GetSFJV(dataObject, "clientName");
            String clientNumber = KHUtil.GetSFJV(dataObject, "clientNumber");
            String callType = KHUtil.GetSFJV(dataObject, "callType");
            String startTime = KHUtil.GetSFJV(dataObject, "startTime");
            String endTime = KHUtil.GetSFJV(dataObject, "endTime");
            String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
            String bridgeDuration = KHUtil.GetSFJV(dataObject, "bridgeDuration");
            String totalDuration = KHUtil.GetSFJV(dataObject, "totalDuration");
            String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
            String status = KHUtil.GetSFJV(dataObject, "status");
            String statusCode = KHUtil.GetSFJV(dataObject, "statusCode");
            String endReason = KHUtil.GetSFJV(dataObject, "endReason");
            String sipCause = KHUtil.GetSFJV(dataObject, "sipCause");
            String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");
            String userField = KHUtil.GetSFJV(dataObject, "userField");
            String xnumber = KHUtil.GetSFJV(dataObject, "xnumber");
            String mark = KHUtil.GetSFJV(dataObject, "mark");
            String tags = KHUtil.GetSFJV(dataObject, "tags");
            String qno = KHUtil.GetSFJV(dataObject, "qno");
            String qname = KHUtil.GetSFJV(dataObject, "qname");
            String markData = KHUtil.GetSFJV(dataObject, "markData");
            String customerRingingTime = KHUtil.GetSFJV(dataObject, "customerRingingTime");
            String queueAnswerInTime = KHUtil.GetSFJV(dataObject, "queueAnswerInTime");
            String evaluation = KHUtil.GetSFJV(dataObject, "evaluation");
            String businessIds = KHUtil.GetSFJV(dataObject, "businessIds");
            String ticketIds = KHUtil.GetSFJV(dataObject, "ticketIds");
            // log.debug("Deal Data uniqueId:" + uniqueId);

            // 检查当前数据库是否存在 存在更新 不存在新建
            sql = "Select Count(*) As Count From clink_list_cdr_obs Where uniqueId = ?";
            sqlResult = jt.queryForList(sql, uniqueId);
            Countnum = sqlResult.get(0).get("Count");
            // log.debug("3-呼出通话记录 检查当前数据库是否存在:" + Countnum);

            if (Countnum <= 0) {

                sql = '''INSERT INTO clink_list_cdr_obs (callId, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, mark, tags, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation, businessIds, ticketIds) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

                jt.update(sql, callId, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, mark, tags, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation, businessIds, ticketIds);

            } else {
                sql = '''UPDATE clink_list_cdr_obs SET callId = ?, hotline = ?, customerNumber = ?, customerNumberEncrypt = ?, customerProvince = ?, customerCity = ?, cno = ?, clientName = ?, clientNumber = ?, callType = ?, startTime = ?, endTime = ?, bridgeTime = ?, bridgeDuration = ?, totalDuration = ?, ivrName = ?, status = ?, statusCode = ?, endReason = ?, sipCause = ?, recordFile = ?, userField = ?, xnumber = ?, mark = ?, tags = ?, qno = ?, qname = ?, markData = ?, customerRingingTime = ?, queueAnswerInTime = ?, evaluation = ?, businessIds = ?, ticketIds = ? WHERE uniqueId = ?''';

                jt.update(sql, callId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, mark, tags, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation, businessIds, ticketIds, uniqueId);
            }
            toDoOBGetd(jt, uniqueId);
        }

    }

    // 4-呼出通话记录明细
    static toDoOBGetd(JdbcTemplate jt, String uniqueId) {

        //处理主数据
        log.debug("4-呼出通话记录明细 toDoGet Data uniqueId:" + uniqueId);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 封装调用头Heads
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/json");

        // 系统接入-GET
        String getPath = "/cc/describe_cdr_ob?";

        Map<String, String> queryParams = new HashMap<String, String>();
        queryParams.put("mainUniqueId", uniqueId);
        String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
        // log.debug("系统接入 doGETRes:" + doGETRes);

        JSONObject dataObject = JSONObject.fromObject(doGETRes).getJSONObject("cdrOb");
        log.debug("4-呼出通话记录明细 Data Result:" + dataObject);

        String callId = KHUtil.GetSFJV(dataObject, "callId");
        String hotline = KHUtil.GetSFJV(dataObject, "hotline");
        String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
        String customerNumberEncrypt = KHUtil.GetSFJV(dataObject, "customerNumberEncrypt");
        String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
        String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
        String cno = KHUtil.GetSFJV(dataObject, "cno");
        String clientName = KHUtil.GetSFJV(dataObject, "clientName");
        String clientNumber = KHUtil.GetSFJV(dataObject, "clientNumber");
        String callType = KHUtil.GetSFJV(dataObject, "callType");
        String startTime = KHUtil.GetSFJV(dataObject, "startTime");
        String endTime = KHUtil.GetSFJV(dataObject, "endTime");
        String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
        String bridgeDuration = KHUtil.GetSFJV(dataObject, "bridgeDuration");
        String totalDuration = KHUtil.GetSFJV(dataObject, "totalDuration");
        String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
        String status = KHUtil.GetSFJV(dataObject, "status");
        String statusCode = KHUtil.GetSFJV(dataObject, "statusCode");
        String endReason = KHUtil.GetSFJV(dataObject, "endReason");
        String sipCause = KHUtil.GetSFJV(dataObject, "sipCause");
        String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");
        String userField = KHUtil.GetSFJV(dataObject, "userField");
        String xnumber = KHUtil.GetSFJV(dataObject, "xnumber");
        String investigation = KHUtil.GetSFJV(dataObject, "investigation");
        String tags = KHUtil.GetSFJV(dataObject, "tags");
        String mark = KHUtil.GetSFJV(dataObject, "mark");
        String qno = KHUtil.GetSFJV(dataObject, "qno");
        String qname = KHUtil.GetSFJV(dataObject, "qname");
        String markData = KHUtil.GetSFJV(dataObject, "markData");
        String customerRingingTime = KHUtil.GetSFJV(dataObject, "customerRingingTime");
        String queueAnswerInTime = KHUtil.GetSFJV(dataObject, "queueAnswerInTime");
        String evaluation = KHUtil.GetSFJV(dataObject, "evaluation");
        // log.debug("4-呼出通话记录明细 Deal Data uniqueId:" + uniqueId);

        // 检查当前数据库是否存在 存在更新 不存在新建
        String sql = "Select Count(*) As Count From clink_describe_cdr_ob Where uniqueId = ?";
        List sqlResult = jt.queryForList(sql, uniqueId);
        int Countnum = sqlResult.get(0).get("Count");
        // log.debug("4-呼出通话记录明细 检查当前数据库是否存在:" + Countnum);

        if (Countnum <= 0) {

            sql = '''INSERT INTO clink_describe_cdr_ob (callId, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, investigation, tags, mark, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

            jt.update(sql, callId, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, investigation, tags, mark, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation);
        }

    }


    // 重复来电数据处理
    public static void callRepeatFix(JdbcTemplate jt, String UniqueID) {

        try {
            log.debug("重复来电数据处理 UniqueID:" + UniqueID);

            // 处理主数据
            String dealSQL = '''Update Clink_List_Cdr_Ibs R1,
       (Select R1.Uniqueid,
               R1.Customernumber,
               R1.Starttime,
               Max(If(R4.Qno != '' And R4.Qno Is Not Null And R4.clientRingingTime != '' And R4.status = '座席未接听' And R4.Starttime > R1.Starttime - 1 * 60 * 60, R4.Is_Call_Fail, 0)) As Is_Call_Fail,
               Max(If(R4.Qno != '' And R4.Qno Is Not Null And R4.clientRingingTime='' And R4.mark != 11 And R4.Starttime > R1.Starttime - 1 * 60 * 60, R4.Is_TimeOut_Fail, 0)) As Is_TimeOut_Fail,
               Max(If(R4.Qno != '' And R4.Qno Is Not Null And R4.clientRingingTime='' And R4.mark = 11 And R4.Starttime > R1.Starttime - 1 * 60 * 60, R4.Is_Abandon_Fail, 0)) As Is_Abandon_Fail,
               Max(R2.Starttime) As Last_Call_Time,
               Max(Ifnull(R2.Starttime, '')) As Is_24flag,
               Max(Ifnull(R3.Starttime, '')) As Is_48flag
          From Clink_List_Cdr_Ibs R1
          Left Join Clink_List_Cdr_Ibs R2
            On R1.Customernumber = R2.Customernumber
           And R1.Starttime > R2.Starttime
           And R2.Starttime > R1.Starttime - 24 * 60 * 60
          Left Join Clink_List_Cdr_Ibs R3
            On R1.Customernumber = R3.Customernumber
           And R1.Starttime > R3.Starttime
           And R3.Starttime > R1.Starttime - 48 * 60 * 60
          Left Join Clink_List_Cdr_Ibs R4
            On R1.Customernumber = R4.Customernumber
           And R1.Starttime > R4.Starttime
         Where 1 = 1
           And R1.Uniqueid = ?
         Group By R1.Uniqueid, R1.Customernumber, R1.Starttime) R2
   Set R1.Is_Call_Fail   = If(R2.Is_Call_Fail <= 0 And R1.Qno != '' And R1.Qno Is Not Null And R1.Clientringingtime Is Not Null And R1.Status = '座席未接听',
                              1,
                              0),
       R1.Is_TimeOut_Fail   = If(R2.Is_TimeOut_Fail <= 0 And R1.Qno != '' And R1.Qno Is Not Null And R1.clientRingingTime='' And R1.mark != 11,
                              1,
                              0),
       R1.Is_Abandon_Fail   = If(R2.Is_Abandon_Fail <= 0 And R1.Qno != '' And R1.Qno Is Not Null And R1.clientRingingTime='' And R1.mark = 11,
                              1,
                              0),
       R1.Last_Call_Time = From_Unixtime(R2.Last_Call_Time),
       R1.Is_24repeat    = If(R2.Is_24flag Is Not Null And R2.Is_24flag != '', 1, 0),
       R1.Is_48repeat    = If(R2.Is_48flag Is Not Null And R2.Is_48flag != '', 1, 0),
       R1.Is_Ignore_Flag   = If((R2.Is_TimeOut_Fail > 0 Or R2.Is_Abandon_Fail > 0 or R2.Is_Call_Fail > 0) And ((R1.Qno != '' And R1.Qno Is Not Null And R1.clientRingingTime='' And (R1.mark != 11 Or R1.mark = 11)) or (R1.Qno != '' And R1.Qno Is Not Null And R1.Clientringingtime Is Not Null And R1.Status = '座席未接听')), 1, 0)
 Where R1.Uniqueid = R2.Uniqueid
   And R1.Uniqueid = ?''';
            int updateNum = jt.update(dealSQL, UniqueID, UniqueID);

            log.debug("重复来电数据处理(" + UniqueID + ") 处理数量：" + updateNum);

        } catch (Exception e) {
            log.error("重复来电数据处理错误", e);
        }

    }

    static String stampToDate(String s) {
        String res = s.length() > 10 ? s : s + "000";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        long lt = new Long(res);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

}